package ui.global;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.widgets.Label;
import Crawler_data.Crawler_data;
import Crawler_data.LoadData;
import DAO.L_likeDAO;
import DAO.UserDAO;
import Download.CancelDelete;
import Download.FavorDow;
import Download.LoadUser;
import Music_Playing.Music_Playing;
import each_class.Album;
import each_class.RanKingClass;
import each_class.SongClass;
import each_class.User;
import ui.LrcView;
import ui.MusicInfo;
import ui.PlayList;
import ui.PlayProgressBar;
import ui.SearchBar;
import ui.SwitchButton;
import ui.TimeView;
import ui.VolumeBar;

public class Operation {
	private static PlayList playList;
	private static SearchBar searchBar;
	private static MusicInfo musicInfo;
	private static PlayProgressBar playProgressBar;
	private static VolumeBar volumeBar;
	private static TimeView timeView;
	private static LrcView lrcView;
	private static Label musicCover;
	private static SwitchButton favoriteButton;
	private static SwitchButton downloadButton;
	private static SwitchButton play_pauseButton;


	public static void initail(List<SongClass> playList) {
		checkNotNull(Operation.playList);
		checkNotNull(playList);
		Operation.playList.initail(playList);
	}



	public static void add_PlayMusic(SongClass music) {
		checkNotNull(playList);
		checkNotNull(music);
		playList.addMusic(music);
	}

	public static void _continue() {
		checkNotNull(playList);
		playList._continue();
	}

	public static void pause() {
		checkNotNull(playList);
		playList.pause();
	}

	public static void stop() {
		checkNotNull(playList);
		playList.stop();
	}

	public static void nextMusic() {
		checkNotNull(playList);
		playList.nextMusic();
	}

	public static void preiousMusic() {
		checkNotNull(playList);
		playList.previous();
	}

	public static void search(String search) {
		checkNotNull(searchBar);
		checkNotNull(search);
		searchBar.setSearchText(search.substring(0, search.length() - 1));
		searchBar.search(search.substring(0, search.length() - 1));
	}

	public static List<SongClass> searchMore() {
		checkNotNull(searchBar);
		return searchBar.searchMore();
	}

	//获取专辑
	public static Album getAlbum(SongClass music) {
		// music不为null，已开线程 TODO
		
		return Crawler_data.Get_album(music.getAlbum_id(), music.getAlbum_name());
	}
	
	// 获取收藏歌曲
	@SuppressWarnings("unchecked")
	public static List<SongClass> getFavorite() {
		// 以确保已登录，已开线程 TODO

		try {
			return (List<SongClass>) LoadUser.loadILike(L_likeDAO.findsong(Current.getCurrentUser().getUser_id()),
					Current.getCurrentUser());
		} catch (IOException e) {
		}
		return null;
	}

	// 获取本地歌曲
	public static List<SongClass> getLoaclMusic() {
		// 已开线程 TODO
		try {
		return	LoadUser.loadRKCSongs(Current.getCurrentUser(), Current.getCurrentUser().getUser_id());
		} catch (IOException e) {
		} 
		return null;
	}
	// 修改用户信息操作 线程已开
		public static User updateUserInof(String name, String passWord, String avatar) {
			try {
				if(name != null) {
					UserDAO.updateName(Current.getCurrentUser().getUser_id(), name);
				Current.getCurrentUser().setUname(name);
				}
				if(passWord != null) {
						UserDAO.updatePwd(Current.getCurrentUser().getUser_id(), passWord);
				Current.getCurrentUser().setUpassword(passWord);
				}
			if(avatar != null) {
				File file = new File(avatar);
				InputStream in = new FileInputStream(file);
				byte[] bt = new byte[(int) file.length()];
				in.read(bt);
				UserDAO.updateImage(Current.getCurrentUser().getUser_id(), bt);

				Current.getCurrentUser().setUser_imagepath(in);
			}
				
			} catch (Exception e) {
			}

			return Current.getCurrentUser();
		}
	
	//获取用户头像 线程已开
	public static InputStream getUserAvatar(User user) {
		
		return Current.getCurrentUser().getUser_imagepath();
	}
	
	// 注册操作
	public static User regist(String name, String passWord, String avatar) {
		try {
			File file = new File(avatar);
			@SuppressWarnings("resource")
			InputStream in = new FileInputStream(file);
			byte[] bt = new byte[(int) file.length()];
			in.read(bt);
			if (UserDAO.UserAdd(name, passWord, bt) == 0) {

				return null;
			} else {
				User user = LoadUser.loadUser(UserDAO.finduser(name, passWord));
				return login(user.getUser_id(), user.getUpassword());
			}
		} catch (IOException e) {
		} catch (Exception e) {
		}
		return null;

	}

	// 登录操作
	public static User login(String acount, String passWord) throws IOException {

		// passWord)).toString());
		return LoadUser.loadUser(UserDAO.login(acount, passWord));
	}

	// 获取歌单封面
	public static InputStream getMusicListCover(RanKingClass musicList) {
		// TODO 已开线程
		try {
			return Crawler_data.Get_image(musicList.getRanKing_image_url());
		} catch (Exception e) {
		}
		return null;
	}

	// 设置歌词
	public static void setLrc(String Lrc) {
		lrcView.setLrc(Lrc);
	}

	public static String getLrc() {
		return lrcView.getLrc();
	}

	public static Map<String, List<RanKingClass>> getMusicStation() {
		// TODO 官方歌单， 推荐歌单 //已开线程

		return LoadData.home_playlist();
	}

	// 禁音
	public static void silence() {
		try {
			Music_Playing.openMute();
		} catch (NullPointerException e) {
		}
	}

	// 恢复音量
	public static void recoverVolume() {
		try {
			Music_Playing.openVoice();
		} catch (NullPointerException e) {
		}
	}

	// 获取音乐封面
	public static InputStream loadMusicCover(SongClass music) {
		try {
			return Crawler_data.Get_image(music);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 收藏音乐
	public static void favorite(SongClass music) {
		if (music != null && music != null) {
			// TODO
			music.setFavorite(true);
			new Thread() {
				@Override
				public void run() {
					try {
						FavorDow.favorDow(Current.getCurrentUser(), music);
					} catch (Exception e) {
					}
				}
			}.start();

		}
	}

	// 取消收藏
	public static void unFavorite(SongClass music) {
		if (music != null) {
			music.setFavorite(false);
			new Thread() {
				@Override
				public void run() {
					try {
						CancelDelete.cancelLike(music, Current.getCurrentUser());

					} catch (Exception e) {
					}
				}
			}.start();

		}
	}

	// 下载音乐
	public static void downLoad(SongClass music) {
		if (music != null) {
			music.setDownload(true);
			new Thread() {
				public void run() {

					try {
						FavorDow.favorDow(Current.getCurrentUser(), music);
					} catch (Exception e) {
					}

				};
			}.start();

		}
	}

	// 删除音乐
	public static void delete(SongClass music) {
		if (music != null) {
			music.setDownload(false);
			new Thread() {
				@Override
				public void run() {
					try {

						CancelDelete.disSong(music, Current.getCurrentUser());

					} catch (Exception e) {
					}
				}
			}.start();

		}
	}

	public static PlayList getPlayList() {
		return playList;
	}

	public static void setPlayList(PlayList playList) {
		Operation.playList = playList;
	}

	public static SearchBar getSearchBar() {
		return searchBar;
	}

	public static void setSearchBar(SearchBar searchBar) {
		Operation.searchBar = searchBar;
	}

	public static MusicInfo getMusicInfo() {
		return musicInfo;
	}

	public static void setMusicInfo(MusicInfo musicInfo) {
		Operation.musicInfo = musicInfo;
	}

	public static PlayProgressBar getPlayProgressBar() {
		return playProgressBar;
	}

	public static void setPlayProgressBar(PlayProgressBar playProgressBar) {
		Operation.playProgressBar = playProgressBar;
	}

	public static VolumeBar getVolumeBar() {
		return volumeBar;
	}

	public static void setVolumeBar(VolumeBar volumeBar) {
		Operation.volumeBar = volumeBar;
	}

	public static TimeView getTimeView() {
		return timeView;
	}

	public static void setTimeView(TimeView timeView) {
		Operation.timeView = timeView;
	}

	public static LrcView getLrcView() {
		return lrcView;
	}

	public static void setLrcView(LrcView lrcView) {
		Operation.lrcView = lrcView;
	}

	public static Label getMusicCover() {
		return musicCover;
	}

	public static void setMusicCover(Label musicCover) {
		Operation.musicCover = musicCover;
	}

	public static SwitchButton getFavoriteButton() {
		return favoriteButton;
	}

	public static void setFavoriteButton(SwitchButton favoriteButton) {
		Operation.favoriteButton = favoriteButton;
	}

	public static SwitchButton getDownloadButton() {
		return downloadButton;
	}
	
	public static void setDownloadButton(SwitchButton downloadButton) {
		Operation.downloadButton = downloadButton;
	}
	
	public static SwitchButton getPlay_pauseButton() {
		return play_pauseButton;
	}



	public static void setPlay_pauseButton(SwitchButton play_pauseButton) {
		Operation.play_pauseButton = play_pauseButton;
	}
	
	private static void checkNotNull(Object target) {
		if (target == null) {
			new RuntimeException("对象未初始化！");
		}
	}
}
